package com.xmx.common.utils;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import jdk.nashorn.internal.parser.JSONParser;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.apache.logging.log4j.message.MapMessage.MapFormat.JSON;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2021/8/11 14:29
 */
public class ListFromEntity2VoUtils {


    public static <T> List<T> entity2Vo(List<Map> source, Class<? extends T> targetClazz) {
        List<T> targetList = new ArrayList();
        Field[] targetFields = targetClazz.getDeclaredFields();
       Field.setAccessible(targetFields,true);
        try {
            for (Map el : source) {
                T target = targetClazz.newInstance();
                for (Field targetField : targetFields) {
                    Object value = el.get(targetField.getName());
                    //double类型转为BigDecimal
                    if(targetField.getType().equals(BigDecimal.class)){
                        if(value!=null) {
                            targetField.set(target, new BigDecimal(String.valueOf(value)));
                            continue;
                        }
                    }
                    //double类型转为BigDecimal
                    if(targetField.getType().equals(BigDecimal.class)){
                        if(value!=null) {
                            targetField.set(target, new BigDecimal(String.valueOf(value)));
                            continue;
                        }
                    }
                    //intger类型转boolean

                    if(targetField.getName().equals("isNew")){
                        targetField.set(target,((Integer)value).equals(new Integer(1))?true:false);
                        continue;
                    }
                    //intger类型转boolean
                    if(targetField.getName().equals("isHot")){
                        targetField.set(target,((Integer)value).equals(new Integer(1))?true:false);
                        continue;
                    }
                    if(targetField.getName().equals("isOnSale")){
                        targetField.set(target,((Integer)value).equals(new Integer(1))?true:false);
                        continue;
                    }

                    //intger类型转boolean
                    if(targetField.getName().equals("deleted")){
                        if ((value.equals(0)||value.equals(1))&&value!=null&&!targetClazz.getName().equals("com.xmx.ndmallwxapi.vo.cart.CartAddVo"))
                        targetField.set(target,((Integer)value).equals(new Integer(1))?true:false);
                        continue;
                    }
                    //集合数据的填充
                    if(targetField.getType().equals(List.class) && targetField.getName().equals("goodsList")){
                        List<?> objects = entity2Vo((List<Map>) value, Class.forName("com.xmx.ndmallwxapi.vo.goods.NewGoodsVo"));
                        targetField.set(target,objects);
                        continue;
                    }
                    if(targetField.getName().equals("specifications")||targetField.getName().equals("gallery")){
//                        String[] substring = value.toString().substring(1,value.toString().lastIndexOf("]")).split(",");
//                        String a[] = new String[substring.length];
//                        for (int i = 0; i < substring.length; i++) {
//                            a[i] = substring[i].substring(1,substring[i].lastIndexOf("\""));
//                        }
                        if(!targetClazz.getName().equals("com.xmx.ndmallwxapi.vo.cart.CartAddVo")){
                            JSONArray array = JSONUtil.parseArray(value);
                            targetField.set(target,array.toArray(new String[]{}));
                        }

                        continue;

                    }
                    targetField.set(target,value);
                }
                targetList.add(target);
            }
        } catch (InstantiationException  e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return targetList;
    }


}
















